/*
 *  FILE: 
 *  	spp.c
 *
 *  DESCRIPTION:
 *      Official Implementation of the Simple Peer Protocol.
 *
 *  FUNCTIONS:
 *      ...
 *
 *
 * Copyright (c) 2006 Karlan Thomas Mitchell<karlanmitchell@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *         This product includes software developed by
           Karlan Thomas Mitchell<karlanmitchell@comcast.net>
 * 4. Neither Karlan Thomas Mitchell nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY KARLAN THOMAS MITCHELL AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "spp.h"

/*Get/Set password, password should be NULL to retrive current password*/
char * spp_ultra_pass(struct st_spp_ctx * spp_data, char * password){

	/*Get password*/
	if(password == NULL)
		return spp_data->ultra.password;


	/*Set password*/
	strlcpy(spp_data->ultra.password, password, 100);
	return password;
	}


/*Get/Set password, password should be NULL to retrive current password*/
char * spp_client_pass(struct st_spp_ctx * spp_data, char * password){

	/*Get password*/
	if(password == NULL)
		return spp_data->client.password;


	/*Set password*/
	strlcpy(spp_data->client.password, password, 100);
	return password;
	}
/*Get/Set userid, userid should be NULL to retrive current userid*/
char * spp_userid(struct st_spp_ctx * spp_data, char * userid){

	/*Get password*/
	if(userid == NULL)
		return spp_data->userid;


	/*Set password*/
	strlcpy(spp_data->userid, userid, 100);
	return userid;
	}

/*Get/Set address, address should be NULL to retrive current IP address*/
char * spp_address(struct st_spp_ctx * spp_data, char * address){

	/*Get password*/
	if(address == NULL)
		return spp_data->address;


	/*Set password*/
	strlcpy(spp_data->address, address, 16);
	return address;
	}

/*Simple Peer Protocol/File Service Bind*/
/*Every client must have this or risk being*/
/*kicked from the network.*/
int spp_fs_bind(struct st_spp_ctx * spp_data){
	return 0;
	}




/*Simple Peer Protocol/Routing Service Bind
 *Everyone with a public IP should run this.
 *This is what enables P2P searchs, PUSHes, chats, 
 *and other broadcast type messages.*/
int spp_rs_bind(struct st_spp_ctx * spp_data){
	return 0;
	}




int spp_readpacket(struct st_psocket * socket_data, struct st_spp_packet * protocol_data, long timeout){
	size_t buffer_lens = 4096;
	char * name_buf   = (char *)malloc( buffer_lens * sizeof(char) );
	char * value_buf  = (char *)malloc( buffer_lens * sizeof(char) );
	char * line_buf   = (char *)malloc( 2 * buffer_lens *  sizeof(char) );
	ssize_t return_val;
	int function_return = 0;	
	int numof_lines = 0;

	struct st_timeval time_data;
	time_data.tv_sec = timeout;

	if(name_buf  == NULL || 
	   value_buf == NULL || 
           line_buf  == NULL)
		return -1;


	while(1){
		errno = 0;
		if(timeout != 0)
			return_val = psocket_readline_r(socket_data, line_buf, buffer_lens * 2);
		else
			return_val = psocket_readline_r_timeout(socket_data, line_buf, buffer_lens * 2, &time_data);

		printf("returnval %d\n", return_val);

		if(return_val < 0){
			function_return = -1;
			break;
			}

		if(return_val == 0){
  			break;
			}
		
		if(str_splitup(line_buf, name_buf, buffer_lens, value_buf, buffer_lens, ":") != -1){
                mdb_set(protocol_data, name_buf, value_buf);
                #ifdef SPP_DEBUG
 		               printf("%s: %s\n", name_buf, value_buf);
                #endif 
                }
		numof_lines++;
		if(numof_lines >= 30)
			break;
	
	}
                #ifdef SPP_DEBUG
		if(numof_lines > 0)
 		               printf("\n");
                #endif 


	free(name_buf);
	free(value_buf);
	free(line_buf);
	return function_return;
	}
